package one.stand.controller;

import com.jmp.autocofiguration.util.OssUtil2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.model.ResultModel;
import one.stand.util.AssertUtil;
import one.stand.util.DesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * @author Administrator
 */
@Api(description = "上传")
@RestController
@RequestMapping("file")
@Slf4j
public class OssController {
    private DesUtil desUtil;

    @PostConstruct
    public void init() throws Exception {
        desUtil = new DesUtil("lXySzByz", "lXySzByz");
    }

    @PostMapping(value = "/uploadObjectOSS", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
    })
    public ResultModel<String> uploadObjectOSS(MultipartFile file) throws IOException {
        ResultModel<String> resultModel = check(file);
        if (Objects.nonNull(resultModel)) return resultModel;

        String url = OssUtil2.uploadByFileName(file.getInputStream(), file.getOriginalFilename());
        url = StringUtils.replaceAll(url, OssUtil2.getWebsiteUrl(), StringUtils.EMPTY);
        return ResultModel.success(url);
    }

    @PostMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
    })
    public ResultModel<String> upload(MultipartFile file) throws IOException {
        ResultModel<String> resultModel = check(file);
        if (Objects.nonNull(resultModel)) return resultModel;

        String url = OssUtil2.uploadByFileName(file.getInputStream(), file.getOriginalFilename());
        return ResultModel.success(url);
    }

    @PostMapping(value = "/upload/encode",
            headers = "content-type=multipart/form-data",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "上传文件、加密")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
    })
    public ResultModel<String> uploadEncode(MultipartFile file) throws IOException {
        ResultModel<String> resultModel = check(file);
        if (Objects.nonNull(resultModel)) return resultModel;

        InputStream inputStream = file.getInputStream();
        byte[] data = new byte[inputStream.available()];
        inputStream.read(data);
        try {
            byte[] dataEncode = desUtil.encodeByte(data);
            InputStream inputStreamEncode = new ByteArrayInputStream(dataEncode);
            String url = OssUtil2.upload(inputStreamEncode, OssUtil2.FileSuffix.thirdD, false);
            AssertUtil.nonNull(url, "上传失败");
            return ResultModel.success(url);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return ResultModel.fail("上传失败");
    }

    private ResultModel<String> check(MultipartFile file) {
        if (file.isEmpty()) {
            return ResultModel.fail("文件为空");
        }
        Long size = file.getSize();
        Long fileSize = 524288000L;//500M
        if (size > fileSize) {
            log.info("文件过大: {}", size);
            return ResultModel.fail("文件过大");
        }
        return null;
    }
}
